package antrace;

public class BonusGameLayer extends AbstractGameLayer {
	
	public static final int APPLE = 1;
	public static final int SPEEDBOOT = 2;
	static int blockWidth = 36;
	private AntSprite sprite;
	
	private byte doOnce = 0;
	
	public BonusGameLayer(AntSprite sprite) {

		super(blockWidth*3, 12,  AntRace.midlet.loadImage("/images/tiles/bonustiles.png").image, 20, 20);
		
		this.sprite = sprite;
		
		this.createTiledLayerFromArray(BlockGenerator.getInstance().getBonusArray(), blockWidth);
		
		this.setPosition(0, 80);
	}
	
	public void collide(AntSprite sprite) {
		// tar bort bonusen!
		int bonus = getCell(getCurrentCol(sprite), getCurrentRow(sprite));
		
		setCell(getCurrentCol(sprite), getCurrentRow(sprite), 0);
		switch(bonus) {
		case APPLE: //äpple
			sprite.addScore(50);
			break;
		case SPEEDBOOT: //sko
			sprite.addScore(10);
			sprite.achieveBonus(AntSprite.BONUS_BOOT);
			break;
		}
		
	}

	public void reset() {
		this.setPosition(0, 80);
	}
	
	public void update() {
		if( sprite.getWorldX() % blockWidth*20 < blockWidth*20/2 ) { 
			if(sprite.getWorldX() > blockWidth*20/2 && doOnce == 0){
				//lägg till andra blocket
				this.createTiledLayerFromArray(BlockGenerator.getInstance().getBonusArray(), blockWidth, blockWidth);
				doOnce++;
			}
			else if(sprite.getWorldX() > blockWidth*20 && doOnce == 1){
				//lägg till tredje blocket
				this.createTiledLayerFromArray(BlockGenerator.getInstance().getBonusArray(), blockWidth, blockWidth*2);
				doOnce++;
			}
			
			if(this.getX() < -1.5*blockWidth*20){
				this.moveLayerBlocks(BlockGenerator.getInstance().getBonusArray(), blockWidth);
				this.createTiledLayerFromArray(BlockGenerator.getInstance().getBonusArray(), blockWidth, blockWidth*2);
			}
			
		}
	}
}
